import {NestFactory} from '@nestjs/core';
import {AppModule} from './app.module';
import {HttpExceptionFilter} from "./filter/http-exception/http-exception.filter";
import {TransformInterceptor} from "./interceptor/transform.interceptor";
import rateLimit from "express-rate-limit";

async function bootstrap() {
    const app = await NestFactory.create(AppModule);

    // 跨域
    app.enableCors({
        "origin": "*",
        "credentials": true,
        // "allowedHeaders":['Authorization','content-type'],
        "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
        "preflightContinue": false,
        "optionsSuccessStatus": 204

    })

    // 限速
    app.use(
        rateLimit({
            message: {
                data: [],
                code: 429,
                message: '请求太多次，请稍后重试',
            },
            windowMs: 1000,
            max: 1,
        }),
    );

    app.useGlobalInterceptors(new TransformInterceptor());
    app.useGlobalFilters(new HttpExceptionFilter());

    await app.listen(3000);
}

bootstrap();
